/*
 * Creado por NIN
 * User: sebastian.lucas
 * Date: 19/04/2007
 * Time: 9:34
 */

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace CSE
{
	/// <summary>
	/// Description of Utils.
	/// </summary>
	public class Utils
	{
	    /*
	     * LoadBitmapResource
	     */
	    public static Bitmap LoadBitmapResource (string str)
	    {
	        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Utils));
	        return ((System.Drawing.Bitmap)(resources.GetObject(str)));
	    }
	    
	    /*
	     * Trace
	     */
	    public static void Trace (string str)
	    {
#if DEBUG
	        Debug.WriteLine(str);
#endif
	    }
	    
	    /*
	     * MsgBox
	     */
	    public static void MsgBox (string str)
	    {
	    	MessageBox.Show(str, "CSE");
	    }
	    
	    /*
	     * Question
	     */
	    public static bool Question (string str)
	    {
	        if (MessageBox.Show(str, "CSE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
	        {
	            return true;
	        }
	        
	        return false;
	    }
	    
	    /*
	     * OpenDialog
	     */
	    public static string OpenDialog (string filter)
	    {
	        OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = filter;
            ofd.Multiselect = false;
            
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return ofd.FileName;
            }
            
            return null;
	    }
	    
	    /*
	     * SaveDialog
	     */
	    public static string SaveDialog (string filter)
	    {
	        SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = filter;
            
            if (sfd.ShowDialog() == DialogResult.OK)
            {
            	return sfd.FileName;
            }
            
            return null;
	    }

	    /*
	     * GetEncoderInfo
	     */
	    public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
	       int j;
	       ImageCodecInfo[] encoders;
           encoders = ImageCodecInfo.GetImageEncoders();
           for(j = 0; j < encoders.Length; ++j)
           {
              if(encoders[j].MimeType == mimeType)
                 return encoders[j];
           }
           return null;
        }
	    
	    /*
	     * Sign
	     */
	    public static int Sign (int num)
	    {
	        if (num < 0)
	        {
	            return -1;
	        }
	        else if (num > 0)
	        {
	            return 1;
	        }
	        
	        return 0;
	    }
	    
	    /*
	     * String2Int
	     */
	    public static int String2Int (string str)
	    {
	        int value = 0;
	        
	        if (str.Length > 0)
	        {
	            value = int.Parse(str.Trim('%').Trim());
	        }
	        
	        return value;
	    }
	    
	    /*
		 * GetViewportPixel
		 */
		public static int GetViewportPixel (int x, int w, int ox, int zoom)
		{
			int pixsize = 0;
			x -= ox;
			
			if (w <= 0)
			{
				return x;
			}
		
			pixsize = ((w*zoom) * 1) / w;
			return x/pixsize;
		}
	}
}
